package com.junjie.checkin.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.junjie.checkin.entity.Enum.serializer.LowercaseEnumSerializer;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Type;

/**
 * @author 洪浚杰
 * @description
 * @date 2025/6/13 21:04
 */
@Configuration
public class JacksonConfig {

    private final ObjectMapper objectMapper;

    public JacksonConfig(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @PostConstruct
    public void setup() {
        SimpleModule module = new SimpleModule();
        // 使用泛型转换解决 Class<Enum> -> Class<? extends Enum<?>>
        module.addSerializer((Class<? extends Enum<?>>) (Type) Enum.class, new LowercaseEnumSerializer());
        objectMapper.registerModule(module);
    }
}